$(function(){
	init();
	//初始化数据
	function init(){
		//获取擅长领域的数据
		$.get(become_lawyer_url, function(data){
     
	      }, "json").error(function(data) {
	        var status_code = $.parseJSON(data.responseText);
	        layer.msg(status_code.message);
	      }).success(function(data) {
		        var obj = data.data;
		        if(obj.user.city){
					city_Main(city_param,null);
				}
				if(obj.user.district){
					district_Main(district_param,null);
				}
				var work_scope = obj.work_scope;
				var options = '';
				options+='<option value="0">选择类型</option>';
				$.each(work_scope, function(key, item) {
						options+='<option value="'+item.key+'">'+item.value+'</option>';
				});
				$("#work_scope").append(options);
	      })
		province_Main(province_param,null);
		var city = $("#city").val();
		var district = $("#district").val();
	    if(city){
	    	city_Main(city_param,null);
	    }
	    if(district){
	    	district_Main(district_param,null);
	    }
	}
   	//获取城市的数据
	$("#province").change(function(){
		$("#city,#district").empty();
		var province_val =  $(this).val();
		city_Main(null,province_val);
	})

	//获取地区的数据
	$("#city").change(function(){
		$("#district").empty();
		var city_val =  $(this).val();
		district_Main(null,city_val);
	})
		//获取省份的数据
	function province_Main(province,_this){
		var province_url = jumpUrl+"/area/province";
		$.get(province_url, function(data){
          
	      }, "json").error(function(data) {
	        var status_code = $.parseJSON(data.responseText);
	        layer.msg(status_code.message);
	      }).success(function(data) {
	          var obj = data.data;
	          var options = '';
              $.each(obj, function(key, item) {
              		options+='<option value="'+item.key+'">'+item.value+'</option>';
              });
              $("#province").append(options);
              if(province){
              	 $("#province option[value='"+province+"']").attr("selected",true);
              }
             
	      });
	}
   //获取城市的数据
    function city_Main(city,_this){
    	var city_url;
    	if(!_this){
    		city_url = jumpUrl+"/area/"+province_param+"/city";
    	}else{
    		city_url = jumpUrl+"/area/"+_this+"/city";
    	}
		 
		$.get(city_url, function(data){
		  
		  }, "json").error(function(data) {
		    var status_code = $.parseJSON(data.responseText);
		    layer.msg(status_code.message);
		  }).success(function(data) {
		      var obj = data.data;
		      var options = '';
		      $.each(obj, function(key, item) {
		      		options+='<option value="'+item.key+'">'+item.value+'</option>';
		      });
		      $("#city").append(options);
		      if(city){
		      	$("#city option[value='"+city+"']").attr("selected",true);
		      }
		  });
	} 
	//获取地区的数据
	function district_Main(district,_this){
		var county_url;
    	if(!_this){
    		county_url = jumpUrl+"/area/"+city_param+"/district";
    	}else{
    		county_url = jumpUrl+"/area/"+_this+"/district";
    	}
		
		$.get(county_url, function(data){
		  
		  }, "json").error(function(data) {
		    var status_code = $.parseJSON(data.responseText);
		    layer.msg(status_code.message);
		  }).success(function(data) {
		      var obj = data.data;
		      var options = '';
		      $.each(obj, function(key, item) {
		      		options+='<option value="'+item.key+'">'+item.value+'</option>';
		      });
		      $("#district").append(options);
		      if(district){
		      	$("#district option[value='"+district+"']").attr("selected",true);
		      }
		  });
	}
	var selectedV = $(".selectivity span").length;
	if(selectedV >= 3){
		$('#work_scope').css("display","none")
		$(".fl span.mgs").css("color","red");
	}
	//擅长领域 select选中的值
	$('#work_scope').change(function(){ 
		var $this = $(this);
		var workSope = $.trim($this.children('option:selected').val());
		var workText = $.trim($this.children('option:selected').text());
		var selectedVal = $(".selectivity span").length;
		if (workSope != 0) {
			if(selectedVal < 3){
				$this.children('option:selected').remove();
				var str ='<span class="multiple" data-item-id="'+workSope+'">'
	    				str+='<a class="multiple-remove" data-id="'+workText+'">'+workText+'<i class="fa fa-remove">X</i></a>'
	    			str+='</span>';
	    		$(".selectivity").append(str);
	    		if(selectedVal < 2){
					$('#work_scope').css("display","inline-block")
				}else{
					$('#work_scope').css("display","none")
					$(".fl span.mgs").css("color","red");
				}
			}else{
				$(".fl span.mgs").css("color","red");
				$('#work_scope').css("display","none")

			}
		}
	}) 
	//删除 select选中的值
	$(document).on( "click", ".fa-remove", function(e) { 
		var $this = $(this);
		var key_item = $this.parent().parent().attr('data-item-id');
		var value_item = $this.parent().attr('data-id');
		$this.parent().parent().remove();
		var	options ='<option value="'+key_item+'">'+value_item+'</option>';
			$("#work_scope").append(options);
		var selectedVal = $(".selectivity span").length;
		if(selectedVal < 3){
			$(".fl span.mgs").css("color","#999");
			$('#work_scope').css("display","inline-block")
		}else{
			$(".fl span.mgs").css("color","red");
			$('#work_scope').css("display","none")
		}
	});
	//提交审核
	$("#user_subReview").click(function(e){
		var params = getParams("#become_lawyer_form");
			if(!subReviewValid(params)){
				return false;
			}else{
				var loading = layer.msg('加载中...', {icon: 16});
				$.post(become_lawyer_url,params,function(data){

			      },'json').error(function(data) {
			        var status_code = $.parseJSON(data.responseText);
			        layer.msg(status_code.message);
	          		// console.info(params)
			      }).success(function(data) {
			      		layer.close(loading);
			      		layer.msg("提交成功");
			      		window.location.href=user_lawyer_url;
			      });
			}
		stopDefault(e);
	});
	//点击保存判断
	function subReviewValid(params){
		var workSope0 = $(".selectivity .multiple").eq(0).attr("data-item-id");
		var workSope1 = $(".selectivity .multiple").eq(1).attr("data-item-id");
		var workSope2 = $(".selectivity .multiple").eq(2).attr("data-item-id");
		if(params.realname.length < 1){
			layer.msg("请输入您的真实名称！");
			return false;
		}
		// if(params.nikname.length < 1){
		// 	layer.msg("请输入您的昵称！");
		// 	return false;
		// }
		if(params.province == null){
			layer.msg("请选择省份！");
			return false;
		}
		if(params.city == null){
			layer.msg("请选择市区！");
			return false;
		}
		if(params.district == null){
			layer.msg("请选择县！");
			return false;
		}
		// if(params.address.length < 1){
		// 	layer.msg("请选择您所在的详细地址！");
		// 	return false;
		// } 
		if(params.license.length < 1){
			layer.msg("请选择您的律师证号！");
			return false;
		} 
		if(workSope0){
			params['work_scope[0]'] = workSope0
		}else{
			layer.msg("请选择擅长领域！");
			return false;
		}
		if(workSope1){
			params['work_scope[1]'] = workSope1
		}
		if(workSope2){
			params['work_scope[2]'] = workSope2
		}
		// if(params.avatar.length < 1){
		// 	layer.msg("请上头像！");
		// 	return false;
		// }
		if(params.license_file.length < 1){
			layer.msg("请上传律师证照！");
			return false;
		}
		if(params.company_name.length < 1){
			layer.msg("请选择您所在律师事务所的名称！");
			return false;
		}
		if(params.ID_img.length < 1){
			layer.msg("请上传您的身份证！");
			return false;
		}
		return true;
	}
	//上传图片的插件
	//初始化jQuery的文件上传控件
	//上传头像
	$('#avatar_file').fileupload({
			limitMultiFileUploads : 1,
			limitMultiFileUploadSize : 1,
			limitMultiFileUploadSizeOverhead : 512,
			limitConcurrentUploads : 1,
		    sequentialUploads : true,
			headers: 
				{"Accept":"application/json"}, //必须有
	        dataType: "json", //表示返回值类型，不必须
			formData:{
				user_id:userId,
				_token:token
			},
			messages: {uploadedBytes: 'Uploaded bytes exceed file size'},
		    done: function (e, data) {//设置文件上传完毕事件的回调函数  
		    	var obj = data.result;
		    	var avatar = obj.data.file_path;
		    	if(data.result=='error1'){
		    		layer.alert('不允许的文件上传类型，仅允许jpg,png,jpeg,gif格式！', 9, !1);
		    		return false;
		    	}else if(data.result=='error2'){
		    		layer.alert('图片尺寸过大,尺寸需小于1M！', 9, !1);
		    		return false;
		    	}
		    	$("input:hidden[name='avatar']").val(avatar);
		    	$("#avatar_peituImg").attr("src", jumpUrl+'/file/show?path='+avatar);//预览图片
		    	$(".avatar_js_cover").show();

		    },
		    fail: function(e, data) {
	                  //错误提示
	                  console.info(data)
	                  console.info(data.result)
		    }
		});
			//上传律师证
	$('#upload_file').fileupload({
			limitMultiFileUploads : 1,
			limitMultiFileUploadSize : 1,
			limitMultiFileUploadSizeOverhead : 512,
			limitConcurrentUploads : 1,
		    sequentialUploads : true,
			headers: 
				{"Accept":"application/json"}, //必须有
	        dataType: "json", //表示返回值类型，不必须
			formData:{
				user_id:userId,
				_token:token
			},
			messages: {uploadedBytes: 'Uploaded bytes exceed file size'},
		    done: function (e, data) {//设置文件上传完毕事件的回调函数  
		    	var obj = data.result;
		    	var license_file = obj.data.file_path;
		    	if(data.result=='error1'){
		    		layer.alert('不允许的文件上传类型，仅允许jpg,png,jpeg,gif格式！', 9, !1);
		    		return false;
		    	}else if(data.result=='error2'){
		    		layer.alert('图片尺寸过大,尺寸需小于1M！', 9, !1);
		    		return false;
		    	}
		    	$("input:hidden[name='license_file']").val(license_file);
		    	$("#peituImg").attr("src", jumpUrl+'/file/show?path='+license_file);//预览图片
		    	$(".js_cover").show();
		    },
		    fail: function(e, data) {
	                  //错误提示
	                  console.info(data)
	                  console.info(data.result)
		    }
		});

		//上传身份证
	$('#sfz_image_file').fileupload({
			limitMultiFileUploads : 1,
			limitMultiFileUploadSize : 1,
			limitMultiFileUploadSizeOverhead : 512,
			limitConcurrentUploads : 1,
		    sequentialUploads : true,
			headers: 
				{"Accept":"application/json"}, //必须有
	        dataType: "json", //表示返回值类型，不必须
			formData:{
				user_id:userId,
				_token:token
			},
			messages: {uploadedBytes: 'Uploaded bytes exceed file size'},
		    done: function (e, data) {//设置文件上传完毕事件的回调函数  
		    	
		    	var obj = data.result;
		    	// console.info(obj)
		    	var ID_img = obj.data.file_path;
		    	if(data.result=='error1'){
		    		layer.alert('不允许的文件上传类型，仅允许jpg,png,jpeg,gif格式！', 9, !1);
		    		return false;
		    	}else if(data.result=='error2'){
		    		layer.alert('图片尺寸过大,尺寸需小于1M！', 9, !1);
		    		return false;
		    	}
		    	$("input:hidden[name='ID_img']").val(ID_img);
		    	$("#sfz_imageImg").attr("src", jumpUrl+'/file/show?path='+ID_img);//预览图片
		    	$(".sfz_js_cover").show();
		    },
		    fail: function(e, data) {
	                  //错误提示
	                  console.info(data)
	                  console.info(data.result)
		    }
		});

		//上传主页背景图片
	$('#bg_image_file').fileupload({
			limitMultiFileUploads : 1,
			limitMultiFileUploadSize : 1,
			limitMultiFileUploadSizeOverhead : 512,
			limitConcurrentUploads : 1,
		    sequentialUploads : true,
			headers: 
				{"Accept":"application/json"}, //必须有
	        dataType: "json", //表示返回值类型，不必须
			formData:{
				user_id:userId,
				_token:token
			},
			messages: {uploadedBytes: 'Uploaded bytes exceed file size'},
		    done: function (e, data) {//设置文件上传完毕事件的回调函数  
		    	var obj = data.result; 
		    	var bg_image = obj.data.file_path; 
		    	if(data.result=='error1'){
		    		layer.alert('不允许的文件上传类型，仅允许jpg,png,jpeg,gif格式！', 9, !1);
		    		return false;
		    	}else if(data.result=='error2'){
		    		layer.alert('图片尺寸过大,尺寸需小于1M！', 9, !1);
		    		return false;
		    	}
		    	$("input:hidden[name='bg_image']").val(bg_image);
		    	$("#bg_imageImg").attr("src", jumpUrl+'/file/show?path='+bg_image);//预览图片
		    	$(".bg_js_cover").show();
		    },
		    	fail: function(e, data) {
	                  //错误提示
	                  console.info(data)
	                  console.info(data.result)
		    }
		    	/*$("input:hidden[name='bg_image']").val(bg_image);
		    	$("#bg_imageImg").attr("src", jumpUrl+'/'+bg_image); //预览图片
		    },
		    fail: function(e, data) {
	                  //错误提示
	                  console.info('1',data)
	                  console.info('2',data.result)
		    }*/
		});
		/*$('button.cancel').click(function (e) {
		    jqXHR.abort();
		});*/
})
